package com.generic.mybatis.dal.dao.criteria;


import com.generic.mybatis.dal.interceptor.automap.TableMapper;

/**
 * 按字段查询条件
 * 字段名、字段值
 *
 * @author wuaj
 */
public abstract class FieldCriteria implements Criteria {

    protected String field;

    protected Object value;

    public FieldCriteria(String field, Object value) {
        this.field = field;
        this.value = value;
    }

    public String getField() {
        return field;
    }

    public Object getValue() {
        return value;
    }

    protected String getAlias(TableMapper tableMapper) {
        return tableMapper.getPropertyAlias(field);
    }

    protected String getParameterName(TableMapper tableMapper) {
        return tableMapper.getPropertyParamName(field);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        FieldCriteria that = (FieldCriteria) o;

        if (field != null ? !field.equals(that.field) : that.field != null) {
            return false;
        }
        return !(value != null ? !value.equals(that.value) : that.value != null);
    }

    @Override
    public int hashCode() {
        int result = field != null ? field.hashCode() : 0;
        result = 31 * result + (value != null ? value.hashCode() : 0);
        return result;
    }
}
